<template>
  <div>
    <router-view v-if="isRouterAlive" />
  </div>
</template>
<script>
import HelloWorld from './components/HelloWorld.vue'
export default {
  name: 'app',
  components: {
    HelloWorld
  },
  provide(){
    return{
      reload: this.reload,
      downloadFiles: this.downloadFiles,
      dirdown:this.dirdown,
      dirshow:this.dirshow
    }
  },
  data(){
    return{
      isRouterAlive:true
    }
  },
  methods:{
    dirshow(dname) {
      axios({
        url:"http://localhost:8085/user/downDir",
        method:"POST",
        params:{
          "dirname":dname
        },
        responseType:'blob'
      }).then((resp)=>{
        window.open(window.URL.createObjectURL(resp.data),"_blank")
      })
    },
    dirdown(dname){
      var th=this;
      axios({
        url:"http://localhost:8085/user/downDir",
        method:"POST",
        params:{
          "dirname":dname
        },
        responseType:"blob"
      }).then((resp)=>{
        th.downloadFiles(resp.data,dname)
      })

    },
    downloadFiles(data, filename) {
      //接收的是blob，若接收的是文件流，需要转化一下
      //var blob = new Blob([data])
      if (typeof window.chrome !== 'undefined') {
        // Chrome version
        var link = document.createElement('a');
        link.href = window.URL.createObjectURL(data);
        link.download = filename;
        link.click();
      } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
        // IE version
        var blob = new Blob([data], { type: 'application/force-download' });
        window.navigator.msSaveBlob(blob, filename);
      } else {
        // Firefox version
        var file = new File([data], filename, { type: 'application/force-download' });
        window.open(URL.createObjectURL(file));
      }
    },
    reload(){
      this.isRouterAlive=false;
      this.$nextTick(function (){
        this.isRouterAlive=true;
      })
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
